'use strict';

ionicTea.controller('teaFriendsRankCtrl', function ($scope, $state, HttpService,UserService) {

	//切换按钮  功能 待封装
	$scope.isRenQiActive = true;
	$scope.isLiveActive = false;
	$scope.isPointActive = false;
	$scope.isTeaBActive = false;

	$scope.typeName = '人气';

	$scope.loadData = function (type) {
		var callback = {
			success: function success(result) {
				var list = result.result;
				var length = list.length;
				var pos = 0;
				for (; pos < length; pos++) {
					list[pos].pos = pos;

					if ('fans' == type) {
						list[pos].value = list[pos].follow_count;
					} else if ('integral' == type) {
						list[pos].value = list[pos].integral;
					} else if ('coin' == type){
						list[pos].value = list[pos].coin;
					} else {
						list[pos].value = list[pos].blog_count;
					}
				}
				$scope.recList = result.result;
			},
			failed: function failed(err) {}
		};

		HttpService({ name: 'user/ranking', params: { orderby: type }, callback: callback });
	};

	$scope.goToUserDetailPage = function (id) {
		UserService.goToUserDetailPage(id);
	};

	$scope.changeToRenQi = function () {
		$scope.isRenQiActive = true;
		$scope.isLiveActive = false;
		$scope.isPointActive = false;
		$scope.isTeaBActive = false;

		$scope.loadData('fans');
		$scope.typeName = '人气';
	};
	$scope.changeToLive = function () {
		$scope.isRenQiActive = false;
		$scope.isLiveActive = true;
		$scope.isPointActive = false;
		$scope.isTeaBActive = false;

		$scope.loadData('activity');
		$scope.typeName = '茶谈数';
	};
	$scope.changeToPoint = function () {
		$scope.isRenQiActive = false;
		$scope.isLiveActive = false;
		$scope.isPointActive = true;
		$scope.isTeaBActive = false;

		$scope.loadData('integral');
		$scope.typeName = '积分';
	};
	$scope.changeToTeaB = function () {
		$scope.isRenQiActive = false;
		$scope.isLiveActive = false;
		$scope.isPointActive = false;
		$scope.isTeaBActive = true;

		$scope.loadData('coin');
		$scope.typeName = '茶币';
	};
});
ionicTea.controller('teaFriendsRecommendCtrl', function ($scope, $state, HttpService,UserService) {

	//切换按钮  功能 待封装
	$scope.isNewActive = true;
	$scope.isRecommendActive = false;

	$scope.changeToNew = function () {
		$scope.isNewActive = true;
		$scope.isRecommendActive = false;

		var callback = {
			success: function success(result) {
				var list = result.result;
				var length = list.length;
				var pos = 0;
				for (; pos < length; pos++) {
					list[pos].pos = pos;
				}
				$scope.recList = result.result;
			},
			failed: function failed(err) {}
		};

		HttpService({ name: 'user/recuser', params: { orderby: 'rec_time' }, callback: callback });
	};

	$scope.changeToRecommend = function () {
		$scope.isNewActive = false;
		$scope.isRecommendActive = true;

		var callback = {
			success: function success(result) {
				var list = result.result;
				var length = list.length;
				var pos = 0;
				for (; pos < length; pos++) {
					list[pos].pos = pos;
				}
				$scope.recList = result.result;
			},
			failed: function failed(err) {}
		};

		HttpService({ name: 'user/recuser', params: { orderby: 'fans' }, callback: callback });
	};

	$scope.goToUserDetailPage = function (id) {
		UserService.goToUserDetailPage(id);
	};
});
ionicTea.controller('teaFriendsList', function ($scope, $state, UserService, HttpService, $rootScope) {

	$scope.doRefresh = function () {
		var user = UserService.getUserAcceptCache();

		var callback = {
			success: function success(data) {
				$scope.friendsList = data;
				// 停止广播ion-refresher
				$scope.$broadcast('scroll.refreshComplete');
			},
			failed: function failed(err) {
				// 停止广播ion-refresher
				$scope.$broadcast('scroll.refreshComplete');
				alert(err);
			}
		};

		HttpService({ name: 'user/friend/list', params: { user_id: user.id }, callback: callback });
	};

	var searchFriends = function(phoneNum) {

		var user = UserService.getUserAcceptCache();

		var callback = {
			success: function success(data) {
				$scope.friendsList = data.result;
			},
			failed: function failed(err) {
				alert(err);
			}
		};

		HttpService({ name: 'user/friend/find',
			params: {
				user_id: user.id,
				keyword: phoneNum
			}, callback: callback });
	}

	$scope.key_search = function(e){
		var keycode = window.event?e.keyCode:e.which;
		var name = e.target.value;
        if(keycode==13){
            searchFriends(name);
            e.target.value = '';
        }
	};

	$scope.goToChat = function (id) {
		RongCloudLibPlugin.clearMessagesUnreadStatus({
		        conversationType: 'PRIVATE',
		        targetId: id,
		    }, function (ret, err) {
		        // alert(ret.status);
		});
		
		$state.go('chat', { id: id });
	};

	$scope.goToSearchFriends = function() {
		$state.go('friends_search');
	}

	$scope.back = function() {
		$rootScope.back();
	}

	$scope.goToUserDetailPage = function (id) {
		if(id)
		UserService.goToUserDetailPage(id);
	};
});
